package com.eleadmin.ocean.biz.impl;

import com.eleadmin.damon.utils.constant.UserConstant;
import com.eleadmin.damon.utils.results.BaseDao;
import com.eleadmin.damon.utils.results.PageResult2;

import com.eleadmin.damon.utils.utils.Utils;
import com.eleadmin.common.system.entity.User;
import com.eleadmin.ocean.biz.OceanBulkCargoBiz;
import com.eleadmin.ocean.entity.OceanBulkCargo;
import com.eleadmin.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * 数据层实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2023-10-25 15:36:44
 */
@Repository
public class OceanBulkCargoBizImpl extends BaseDao implements OceanBulkCargoBiz {

    @Override
    public JsonResult list(PageResult2<T> page, OceanBulkCargo oceanBulkCargo, User loginUser) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<OceanBulkCargo> rm = new BeanPropertyRowMapper<OceanBulkCargo>(OceanBulkCargo.class);
        StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.customer_id");
        sql.append(",o.customer,o.customer_code,o.services,o.fba");
        sql.append(",o.contacts,o.company,o.phone,o.addr1");
        sql.append(",o.city,o.province,o.country,o.postal");
        sql.append(",o.state,o.apply_customs_type,o.customs_clearance,o.pay_taxes_type");
        sql.append(",o.delivery_terms,o.purchase_insurance,o.box_nums_total,o.box_volume");
        sql.append(",o.box_weight,o.remark,o.create_time,o.create_user");
        sql.append(",o.update_time,o.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName,suser2.nickname updateName ");
        sql.append(" from chf_ocean_bulk_cargo o ");
        sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
        sql.append(" left join sys_user suser2 on suser2.user_id=o.update_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
        sql.append(" where o.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and o.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(oceanBulkCargo)) {
            if (Utils.isNotEmpty(oceanBulkCargo.getCustomer())) {
                sql.append(" and instr(o.customer,:customer)>0 ");
                map.put("customer", oceanBulkCargo.getCustomer());
            }
            if (Utils.isNotEmpty(oceanBulkCargo.getSort()) && Utils.isNotEmpty(oceanBulkCargo.getOrder())) {
                sql.append(" order by o." + oceanBulkCargo.getSort() + " " + oceanBulkCargo.getOrder());
            } else {
                sql.append(" order by o.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<OceanBulkCargo> list(OceanBulkCargo oceanBulkCargo, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<OceanBulkCargo> rm = new BeanPropertyRowMapper<OceanBulkCargo>(OceanBulkCargo.class);
        StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.customer_id");
        sql.append(",o.customer,o.customer_code,o.services,o.fba");
        sql.append(",o.contacts,o.company,o.phone,o.addr1");
        sql.append(",o.city,o.province,o.country,o.postal");
        sql.append(",o.state,o.apply_customs_type,o.customs_clearance,o.pay_taxes_type");
        sql.append(",o.delivery_terms,o.purchase_insurance,o.box_nums_total,o.box_volume");
        sql.append(",o.box_weight,o.remark,o.create_time,o.create_user");
        sql.append(",o.update_time,o.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_ocean_bulk_cargo o ");
        sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
        sql.append(" where o.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and o.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(oceanBulkCargo)) {
            if (Utils.isNotEmpty(oceanBulkCargo.getCustomer())) {
                sql.append(" and instr(o.customer,:customer)>0 ");
                map.put("customer", oceanBulkCargo.getCustomer());
            }
            if (Utils.isNotEmpty(oceanBulkCargo.getSort()) && Utils.isNotEmpty(oceanBulkCargo.getOrder())) {
                sql.append(" order by o." + oceanBulkCargo.getSort() + " " + oceanBulkCargo.getOrder());
            } else {
                sql.append(" order by o.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public OceanBulkCargo getById(String id) {
        RowMapper<OceanBulkCargo> rm = new BeanPropertyRowMapper<OceanBulkCargo>(OceanBulkCargo.class);
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.customer_id");
        sql.append(",o.customer,o.customer_code,o.services,o.fba");
        sql.append(",o.contacts,o.company,o.phone,o.addr1");
        sql.append(",o.city,o.province,o.country,o.postal");
        sql.append(",o.state,o.apply_customs_type,o.customs_clearance,o.pay_taxes_type");
        sql.append(",o.delivery_terms,o.purchase_insurance,o.box_nums_total,o.box_volume");
        sql.append(",o.box_weight,o.remark,o.create_time,o.create_user");
        sql.append(",o.update_time,o.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_ocean_bulk_cargo o ");
        sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
        sql.append(" where o.deleted=0 and o.id=:id ");
        map.put("id", id);
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    @Override
    public boolean save(OceanBulkCargo oceanBulkCargo, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("insert into chf_ocean_bulk_cargo (");
        sql.append("organization_id,customer_id,customer,customer_code");
        sql.append(",services,fba,contacts,company");
        sql.append(",phone,addr1,city,province");
        sql.append(",country,postal,state,apply_customs_type");
        sql.append(",customs_clearance,pay_taxes_type,delivery_terms,purchase_insurance");
        sql.append(",box_nums_total,box_volume,box_weight,remark");
        sql.append(",create_user,create_time) values ( ");
        sql.append(":organization_id,:customer_id,:customer,:customer_code");
        sql.append(",:services,:fba,:contacts,:company");
        sql.append(",:phone,:addr1,:city,:province");
        sql.append(",:country,:postal,:state,:apply_customs_type");
        sql.append(",:customs_clearance,:pay_taxes_type,:delivery_terms,:purchase_insurance");
        sql.append(",:box_nums_total,:box_volume,:box_weight,:remark");
        sql.append(",:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("customer_id", oceanBulkCargo.getCustomerId() == null ? null : oceanBulkCargo.getCustomerId() + "");
        map.put("customer", oceanBulkCargo.getCustomer());
        map.put("customer_code", oceanBulkCargo.getCustomerCode());
        map.put("services", oceanBulkCargo.getServices());
        map.put("fba", oceanBulkCargo.getFba());
        map.put("contacts", oceanBulkCargo.getContacts());
        map.put("company", oceanBulkCargo.getCompany());
        map.put("phone", oceanBulkCargo.getPhone());
        map.put("addr1", oceanBulkCargo.getAddr1());
        map.put("city", oceanBulkCargo.getCity());
        map.put("province", oceanBulkCargo.getProvince());
        map.put("country", oceanBulkCargo.getCountry());
        map.put("postal", oceanBulkCargo.getPostal());
        map.put("state", oceanBulkCargo.getState());
        map.put("apply_customs_type", oceanBulkCargo.getApplyCustomsType());
        map.put("customs_clearance", oceanBulkCargo.getCustomsClearance());
        map.put("pay_taxes_type", oceanBulkCargo.getPayTaxesType());
        map.put("delivery_terms", oceanBulkCargo.getDeliveryTerms());
        map.put("purchase_insurance", oceanBulkCargo.getPurchaseInsurance());
        map.put("box_nums_total", oceanBulkCargo.getBoxNumsTotal() == null ? null : oceanBulkCargo.getBoxNumsTotal() + "");
        map.put("box_volume", oceanBulkCargo.getBoxVolume() == null ? null : oceanBulkCargo.getBoxVolume() + "");
        map.put("box_weight", oceanBulkCargo.getBoxWeight() == null ? null : oceanBulkCargo.getBoxWeight() + "");
        map.put("remark", oceanBulkCargo.getRemark());
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(OceanBulkCargo oceanBulkCargo, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("update chf_ocean_bulk_cargo set ");
        sql.append("customer_id=:customer_id,customer=:customer,customer_code=:customer_code ");
        sql.append(",services=:services,fba=:fba,contacts=:contacts ");
        sql.append(",company=:company,phone=:phone,addr1=:addr1 ");
        sql.append(",city=:city,province=:province,country=:country ");
        sql.append(",postal=:postal,state=:state,apply_customs_type=:apply_customs_type ");
        sql.append(",customs_clearance=:customs_clearance,pay_taxes_type=:pay_taxes_type,delivery_terms=:delivery_terms ");
        sql.append(",purchase_insurance=:purchase_insurance,box_nums_total=:box_nums_total,box_volume=:box_volume ");
        sql.append(",box_weight=:box_weight,remark=:remark,update_time=now() ");
        sql.append(",update_user=:update_user where id=:id ");
        map.put("customer_id", oceanBulkCargo.getCustomerId() == null ? null : oceanBulkCargo.getCustomerId() + "");
        map.put("customer", oceanBulkCargo.getCustomer());
        map.put("customer_code", oceanBulkCargo.getCustomerCode());
        map.put("services", oceanBulkCargo.getServices());
        map.put("fba", oceanBulkCargo.getFba());
        map.put("contacts", oceanBulkCargo.getContacts());
        map.put("company", oceanBulkCargo.getCompany());
        map.put("phone", oceanBulkCargo.getPhone());
        map.put("addr1", oceanBulkCargo.getAddr1());
        map.put("city", oceanBulkCargo.getCity());
        map.put("province", oceanBulkCargo.getProvince());
        map.put("country", oceanBulkCargo.getCountry());
        map.put("postal", oceanBulkCargo.getPostal());
        map.put("state", oceanBulkCargo.getState());
        map.put("apply_customs_type", oceanBulkCargo.getApplyCustomsType());
        map.put("customs_clearance", oceanBulkCargo.getCustomsClearance());
        map.put("pay_taxes_type", oceanBulkCargo.getPayTaxesType());
        map.put("delivery_terms", oceanBulkCargo.getDeliveryTerms());
        map.put("purchase_insurance", oceanBulkCargo.getPurchaseInsurance());
        map.put("box_nums_total", oceanBulkCargo.getBoxNumsTotal() == null ? null : oceanBulkCargo.getBoxNumsTotal() + "");
        map.put("box_volume", oceanBulkCargo.getBoxVolume() == null ? null : oceanBulkCargo.getBoxVolume() + "");
        map.put("box_weight", oceanBulkCargo.getBoxWeight() == null ? null : oceanBulkCargo.getBoxWeight() + "");
        map.put("remark", oceanBulkCargo.getRemark());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", oceanBulkCargo.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(String id, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if (id.length() > 0) {
            for (String id1 : id.split(",")) {
                map = new HashMap<String, String>();
                if (id1 == null || id1.equals("")) {
                    break;
                }
                StringBuffer sql = new StringBuffer("update chf_ocean_bulk_cargo set deleted=1" +
                        ",update_user=:update_user, update_time=now() "
                        + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", id1);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(), map);
            }
        }
        return count > 0;
    }
}
